// $Id: CheckinHandler.java 51 2011-10-21 16:53:27Z huijzer@gmail.com $
package lime.browser.command;

import lime.browser.view.NavigatorView;
import lime.common.LimeException;
import lime.common.UIUtils;
import lime.model.Document;
import lime.model.Folder;
import lime.service.RepositoryService;
import lime.service.SpringApplicationContext;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITreeSelection;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.ui.handlers.HandlerUtil;

import com.documentum.com.DfClientX;
import com.documentum.com.IDfClientX;
import com.documentum.fc.client.IDfSysObject;
import com.documentum.fc.common.DfException;
import com.documentum.fc.common.DfId;
import com.documentum.fc.common.IDfId;
import com.documentum.operations.IDfCheckinNode;
import com.documentum.operations.IDfCheckinOperation;

public class CheckinHandler extends AbstractHandler {

	private RepositoryService repositoryService;

	public CheckinHandler() {
		super();
		repositoryService = (RepositoryService) SpringApplicationContext.getBean("repositoryService");
	}

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		
		String versionParameter = event.getParameter("lime.parameter.version");
		int version = IDfCheckinOperation.NEXT_MINOR;
		if (versionParameter.equals("same")) {
			version = IDfCheckinOperation.SAME_VERSION;
		} else if (versionParameter.equals("major")) {
			version = IDfCheckinOperation.NEXT_MAJOR;
		} 
		
		ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().getSelection();
		if (selection != null && selection instanceof IStructuredSelection) {
			
			ITreeSelection treeSelection = (ITreeSelection) selection;
			Object element = treeSelection.getFirstElement();
			
			if (element != null && element instanceof Document) {
			
				Document document = (Document) element;
				IDfId objectId = new DfId(document.getObjectId());
				
				try {
					IDfSysObject sysObject = (IDfSysObject) repositoryService.getSession().getObject(objectId);
					IDfClientX clientx = new DfClientX();
					IDfCheckinOperation operation = clientx.getCheckinOperation();
					operation.add(sysObject);
					operation.setCheckinVersion(version);
					
					IDfCheckinNode node = (IDfCheckinNode) operation.add(sysObject);
					operation.execute();
					IDfId newObjectId = node.getNewObjectId();
					 
					TreePath path = treeSelection.getPaths()[0];
					TreePath parentPath = path.getParentPath();
					Folder parent = (Folder) parentPath.getLastSegment();
					
					NavigatorView view = (NavigatorView) HandlerUtil.getActivePart(event);
					view.getViewer().refresh(parent);
					 
					Object[] children = parent.getChildren(parent);
					for (Object child : children) {
						if (child instanceof Document) {
							Document doc = (Document) child;
							if (doc.getObjectId().equals(newObjectId.getId())) {	
								TreePath newPath = parentPath.createChildPath(doc);
								ITreeSelection newSelection = new TreeSelection(newPath);
								view.getViewer().setSelection(newSelection, true);
								break;
							}
						}
					}
					 
					ISelectionProvider selectionProvider = HandlerUtil.getActiveSite(event).getSelectionProvider();
					UIUtils.refreshSelectionProvider(selectionProvider);
					
				} catch (DfException e) {
					throw new LimeException(e);
				}
			}
		}
		return null;
	}

}
